package com.flow.modules.dingtalk.controller;

import com.dingtalk.api.response.*;
import com.flow.common.utils.Result;
import com.flow.modules.dingtalk.service.DingTalkAttendanceService;
import com.flow.modules.dingtalk.service.DingTalkInitDeptAndUsersService;
import com.flow.modules.dingtalk.service.DingTalkService;
import com.flow.modules.sys.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuledong
 * @description: TODO
 * @date 2023/4/23 10:48
 */
@RestController
@RequestMapping("/dingtalk/api")
@Api(tags = "钉钉")
public class DingTalkController {

    @Resource
    private DingTalkService dingTalkService;

    @Resource
    private DingTalkInitDeptAndUsersService dingTalkInitDeptAndUsers;

    @Resource
    private DingTalkAttendanceService dingTalkAttendanceService;

    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping("getAccessToken")
    @ApiOperation("获取accessToken")
    public Result getAccessToken() {
        try {
            String accessTokenInfo = dingTalkService.getAccessToken();
            return new Result<String>().ok(accessTokenInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("getDeptListByAccessToken")
    @ApiOperation("根据accessToken获取部门列表")
    public OapiV2DepartmentListsubResponse getDeptListByAccessToken(@RequestParam String accessToken,
                                                                    @RequestParam(defaultValue = "1") Long deptId) {
        return dingTalkService.getDeptListByAccessToken(accessToken, deptId);
    }

    @GetMapping("getDeptInfoById")
    @ApiOperation("根据accessToken、deptId获取部门详细信息")
    public OapiV2UserGetResponse getDeptInfoById(@RequestParam String accessToken,
                                                 @RequestParam String deptId) {
        return dingTalkService.getUserInfoById(accessToken, deptId);
    }

    @GetMapping("getUserCountByAccessToken")
    @ApiOperation("根据accessToken获取员工人数")
    public OapiUserCountResponse getUserCountByAccessToken(@RequestParam String accessToken,
                                                           @RequestParam(defaultValue = "false") Boolean onlyActive) {
        return dingTalkService.getUserCountByAccessToken(accessToken, onlyActive);
    }

    @GetMapping("getUserInfoByDepts")
    @ApiOperation("获取部门用户基础信息")
    public OapiV2UserListResponse getUserInfoByDepts(@RequestParam String accessToken,
                                                     @RequestParam(defaultValue = "1") Long deptId,
                                                     @RequestParam Long cursor,
                                                     @RequestParam Long size) {
        return dingTalkService.getUserInfoByDepts(accessToken, deptId, cursor, size);
    }

    @GetMapping("getUserInfoById")
    @ApiOperation("根据员工id获取员工详细信息")
    public OapiV2UserGetResponse getUserInfoById(@RequestParam String accessToken,
                                                 @RequestParam String userId) {
        return dingTalkService.getUserInfoById(accessToken, userId);
    }


    @GetMapping("synchronousDeptAndUsers")
    @ApiOperation("初始化同步部门及用户")
    public Result synchronousDeptAndUsers() {
        return dingTalkInitDeptAndUsers.synchronousDeptAndUsers();
    }


    @GetMapping("getAttendanceList")
    @ApiOperation("获取考勤打卡详情")
    public OapiAttendanceListRecordResponse getAttendanceList(@RequestParam String accessToken,@RequestParam List<String> userIds,
                                                              @RequestParam String checkDateFrom, @RequestParam String checkDateTo) {
        return dingTalkService.getAttendanceByUserIds(accessToken, userIds, checkDateFrom, checkDateTo);
    }


    @GetMapping("getUserIdsGroup")
    @ApiOperation("获取人员分组（50人一组）")
    public List<List<String>> getUserIdsGroup(@RequestParam String deptId) {
        return dingTalkAttendanceService.getUserIdsGroup(deptId);
    }

    @GetMapping("getAttendanceInfoByDepts")
    @ApiOperation("返回全公司考勤详情")
    public List<List<OapiAttendanceListRecordResponse>> getAttendanceInfoByDepts(@RequestParam String accessToken,List<String> allDeptList,
                                                                                 @RequestParam String checkDateFrom, @RequestParam String checkDateTo) {
        return dingTalkAttendanceService.getAttendanceInfoByDepts(accessToken, allDeptList, checkDateFrom, checkDateTo);
    }


}
